package com.starinside.wp.service.impl;

import com.starinside.wp.domain.Classes;
import com.starinside.wp.domain.TimeLineItem;
import com.starinside.wp.domain.liveRecord.RecordListVo;
import com.starinside.wp.domain.observation.ObservationListItemVO;
import com.starinside.wp.domain.record.LiveRecord;
import com.starinside.wp.foundation.dto.StaffDTO;
import com.starinside.wp.foundation.vo.ChildrenEvoluateRecordVO;
import com.starinside.wp.foundation.vo.ChildrenHealthBaseInfoVO;
import com.starinside.wp.repository.ChildrenObservationRecordRepository;
import com.starinside.wp.repository.LiveRecordRepository;
import com.starinside.wp.repository.HealthBaseInfoNativeRepository;
import com.starinside.wp.repository.LiveRecordNativeRepository;
import com.starinside.wp.service.LiveRecordService;
import com.starinside.wp.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class LiveRecordServiceImpl implements LiveRecordService {

    @Autowired
    LiveRecordNativeRepository liveRecordNativeRepository;

//    @Autowired
//    private EvaluateRecordNativeRepository evaluateRecordNativeRepository;

    @Autowired
    private HealthBaseInfoNativeRepository healthBaseInfoNativeRepository;

    @Autowired
    private ChildrenObservationRecordRepository childrenObservationRecordRepository;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private LiveRecordRepository liveRecordRepository;

    @Override
    public Page<RecordListVo> getLiveRecordsByChildrenId(String childrenId, Integer pageNum, Integer pageSize) {

        List<LiveRecord> list = liveRecordNativeRepository.findLiveRecordByChildrenId(childrenId, pageNum, pageSize);

        Long count = liveRecordNativeRepository.countLiveRecordByChildrenId(childrenId);

        return new PageImpl(list, PageRequest.of(pageNum, pageSize), count);
    }

    @Override
    public Page<LiveRecord> getLiveRecordsByChildrenIds(String[] childrenIds, Integer pageNum, Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        Page<LiveRecord> result = liveRecordRepository.findLiveRecordByChildrenIds(childrenIds, pageRequest);

        return result;
    }

    @Override
    public LiveRecord addLiveRecord(String title, String content, MultipartFile[] files, String childrenId, Integer mediaType, Integer category, String coursePlanId) {
//        String resourceUrl = uploadService.uploadLiveRecordImg(staffId, mediaType, files);
//        StaffDTO staff = ssoCallService.getStaffDTOById(staffId);
//        String headImgUrl =  staff.getHeadImgUrl();
//
//        LiveRecord liveRecord = LiveRecord.of(title, content, category, resourceUrl, "", staffId, staff.getRealName(), areaId, mediaType, coursePlanId, headImgUrl);
//        liveRecordRepository.save(liveRecord);
//
//        return liveRecord;
        return null;
    }

    @Override
    public List<TimeLineItem> getTimeLineItemsByChildrenId(String childrenId, String beforeTime, Integer pageSize){

        List<RecordListVo> lives = liveRecordNativeRepository.findLiveRecordByChildrenIdBeforeTime(childrenId, beforeTime, pageSize);

        String afterTime = null;
        if(lives.size() == pageSize){
            afterTime = lives.get(lives.size()-1).getCreated().toString();
        }

        List<ChildrenHealthBaseInfoVO> healthies = healthBaseInfoNativeRepository.findVOByChildrenId(childrenId, beforeTime, afterTime);

//        List<ChildrenEvoluateRecordVO> evaluates = evaluateRecordNativeRepository.findEvoluatesRecordByChildrenIdBetween(childrenId, beforeTime, afterTime);

        List<ObservationListItemVO> observationListItems = childrenObservationRecordRepository.findByChildrenIdAndTimeBetween(childrenId, beforeTime, afterTime);

        List<TimeLineItem> result = new ArrayList<>(lives.size() + healthies.size() /* + evaluates.size()*/);

        for(RecordListVo vo : lives){
            TimeLineItem item = new TimeLineItem();
            item.setObjId(vo.getId());
            item.setObj(vo);
            item.setCreateAt(vo.getCreated());
            item.setType(1);
            result.add(item);
        }

        for(ChildrenHealthBaseInfoVO vo : healthies){
            TimeLineItem item = new TimeLineItem();
            item.setObjId(vo.getId());
            item.setObj(vo);
            item.setCreateAt(vo.getCreated());
            item.setType(2);
            result.add(item);
        }

//        for(ChildrenEvoluateRecordVO vo : evaluates){
//            TimeLineItem item = new TimeLineItem();
//            item.setObjId(vo.getId());
//            item.setObj(vo);
//            item.setCreateAt(vo.getCreated());
//            item.setType(3);
//            result.add(item);
//        }

        for(ObservationListItemVO vo : observationListItems){
            TimeLineItem item = new TimeLineItem();
            item.setObjId(vo.getId());
            item.setObj(vo);
            item.setCreateAt(vo.getCreated());
            item.setType(4);
            result.add(item);
        }

        result.sort(new Comparator<TimeLineItem>() {
            @Override
            public int compare(TimeLineItem o1, TimeLineItem o2) {
                return o2.getCreateAt().compareTo(o1.getCreateAt());
            }
        });

        return result;
    }

}
